package com.hsm1.test2;

public class Student {
    private String name;
    private String sex;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {

        if ("男".equals(sex) ||"女".equals(sex)) {
            this.sex = sex;
        } else {
            //对于非男女的情况，解决方法一通过if else进行解决
            //System.out.println("对不起，您输入的年龄有误");
            //方法二：通过运行时异常
            //throw new RuntimeException();
            //方法三：通过运行时异常并传入参数来解决
            //throw new RuntimeException("对不起，您输入的性别有误");
            //方法四：通过检查异常;可以抛出，也可以包围
            try {
                throw new MyException("对不起，您输入的信息有误");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Student() {

    }

    public Student(String name, String sex, int age) {
        this.name = name;
        setSex(sex);
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", age=" + age +
                '}';
    }
}
